package free.akerigan.yafb2lib.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import free.akerigan.yafb2lib.library.LibraryModel;
import free.akerigan.yafb2lib.library.LibraryModelFactory;

/**
 * Servlet implementation class for Servlet: LibraryServlet
 * 
 */
public class LibraryServlet extends javax.servlet.http.HttpServlet implements
	javax.servlet.Servlet {

    private static final long serialVersionUID = -4283273964020198603L;

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public LibraryServlet() {
	super();
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	
	ServletContext app = getServletContext();

	LibraryModel library;

	synchronized (app) {
	    library = (LibraryModel) app.getAttribute("library");

	    if (library == null) {
		library = LibraryModelFactory
			.createFromFilesystem(getInitParameter("path"));
		app.setAttribute("library", library);
	    }
	}
	
	RequestDispatcher dispatcher;
	dispatcher = app.getRequestDispatcher("/library.jsp");
	dispatcher.forward(request, response);
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub
    }
}